package com.bt.lms.dao.servise.impl;

import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.RegisterInfo;
import com.bt.lms.dao.servise.StuStatusRegisterInfoDao;
import com.bt.lms.utils.DriverUtils;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;

/**
 * 注册信息dao实现
 * @author 王波
 */
public class StuStatusRegisterInfoDaoImpl implements StuStatusRegisterInfoDao {

    @Override
    public int insert(String sql, RegisterInfo registerInfo) throws SQLException {
        //维护需要手动维护的注册信息
        //没有则不用
        Integer registerState = registerInfo.getState();
        LocalDate registerDate = registerInfo.getDate();
        LocalDate gotoDate = registerInfo.getGotoDate();
        String notRegisterCause = registerInfo.getNotRegisterCause();
        String notRegisterGo = registerInfo.getNotRegisterGo();
        Integer isVacate = registerInfo.getIsVacate();
        String schoolYear = registerInfo.getSchoolYear();
        String semester = registerInfo.getSemester();
        String classroom = registerInfo.getClassroom();


        Connection conn = DriverUtils.Driver();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,registerState);
        ps.setDate(2, Date.valueOf(registerDate));
        ps.setDate(3, Date.valueOf(gotoDate));
        ps.setString(4,notRegisterCause);
        ps.setString(5,notRegisterGo);
        ps.setInt(6,isVacate);
        ps.setString(7,schoolYear);
        ps.setString(8,semester);
        ps.setString(9,classroom);


        int i = ps.executeUpdate();

        //关闭资源
        ps.close();
        conn.close();

        return i;
    }
    @Override
    public boolean update(String sql, RegisterInfo registerInfo,Long id) throws SQLException {

        Integer registerState = registerInfo.getState();
        LocalDate registerDate = registerInfo.getDate();
        LocalDate gotoDate = registerInfo.getGotoDate();
        String notRegisterCause = registerInfo.getNotRegisterCause();
        String notRegisterGo = registerInfo.getNotRegisterGo();
        Integer isVacate = registerInfo.getIsVacate();
        String schoolYear = registerInfo.getSchoolYear();
        String semester = registerInfo.getSemester();
        String classroom = registerInfo.getClassroom();


        Connection conn = DriverUtils.Driver();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,registerState);
        ps.setDate(2, Date.valueOf(registerDate));
        ps.setDate(3, Date.valueOf(gotoDate));
        ps.setString(4,notRegisterCause);
        ps.setString(5,notRegisterGo);
        ps.setInt(6,isVacate);
        ps.setString(7,schoolYear);
        ps.setString(8,semester);
        ps.setString(9,classroom);
        ps.setInt(10, BOConstant.REGISTER_INFO_DELETED_NO);
        ps.setLong(11,id);

        int i = ps.executeUpdate();

        //关闭资源
        ps.close();
        conn.close();
        return true;
    }

    @Override
    public boolean delete(String sql,Long id) throws SQLException {
        Connection conn = DriverUtils.Driver();
        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setInt(1, BOConstant.REGISTER_INFO_DELETED_YES);
        ps.setLong(2,id);

        int i = ps.executeUpdate();

        //关闭资源
        ps.close();
        conn.close();
        return true;
    }
}
